<?php
/**
  * 公共控制器 
  * date 2013-1-17
  * 封装公共方法
  */ 
class CommonAction extends Action{
	  protected $currentModel='';
    protected $pkId='';

    public function _initialize()
    {
      import ( '@.ORG.RBAC');
      if (!RBAC::AccessDecision())
      {
        if(!$_SESSION[C('USER_AUTH_KEY')])
        {
          redirect(PHP_FILE.C('USER_AUTH_GATEWAY'));
        }
        if(C('RBAC_ERROR_PAGE'))
        {
          redirect (C('RBAC_ERROR_PAGE'));
        }else 
            {
              if (C('GUEST_AUTH_ON'))
              {
                $this->assign ('jumpUrl',PHP_FILE.C ('USER_AUTH_GATEWAY'));
              }
              $this->assign('jumpUrl',U('Index/index'));
              $this->error (L ('_VALID_ACCESS_'));
            }
      }     
      $this->pkId = $this->currentModel->getPk();
      $this->assign('menu',R('Public/menu'));
    }

    /**
      * 根据表单生成查询条件
      * 进行列表过滤
      * @access protected
      * @param string $name 数据对象名称
      * @return HashMap
      * @throws ThinkExecption
      */
    protected function _search($name = '') {
        //生成查询条件
        if (empty($name)) 
        {
            $model = $this->currentModel;
        }else
        	{
        		$model = D($name);
        	}
        $map = array();
        foreach ($model->getDbFields() as $key => $val)
        {
            if (isset($_REQUEST [$val]) && $_REQUEST [$val] != '')
            {
                $map [$val] = $_REQUEST [$val];
            }
        }
        return $map;
    }

  /**
    * 分页对象
    * @param $count 总记录数
    * @param $num 一页显示数量
    * @return obj
    */
    protected function _page ($count = 0, $num = 0) {
        import ('@.ORG.Page');
        if ($count == 0 && $num == 0)
        {
          $page = new Page($this->currentModel->count (), C ('PAGE_LIMIT'));
        }  
        else
          {
            $page = new Page($count, $num);
          }
        return $page;
    }

    protected function delUploadFile($type,$file)
    {
      @unlink(C('MY_UPLOAD_PATH').$type.'/'.$file);
    }

  /**
  * 上传缩略图
  * @access protected
  */
  protected function _uploadThumb($config='')
  {
    import('@.ORG.MyUploadFile');
    $upload=new MyUploadFile('thumb');
    $upload->run_upload();
    $picInfo=$upload->getUploadFileInfo();
    if(is_array($picInfo))
    {
      $_POST['thumb']=$picInfo[0]['savename'];
    }
  }    

  /**
   * ajax移除图片
   * @access public 
   */
  public function ajaxDelGallery()
  {
      $config=C('GALLERY_THUMB_SIZE');
      $filename=$this->_get('filename');
      $action=$this->_get('action');
      $galleryTable=$this->_get('galleryTable');
      if($action=='pic')
      {
        $this->delUploadFile('thumb',$config['prefix'].$filename);
        $this->delUploadFile('gallery',$config['prefix'].$filename);
        echo 1;
      }else
          {
            if(M($galleryTable)->where(array('filename'=>$filename))->delete())
            {
              $this->delUploadFile('thumb',$config['prefix'].$filename);
              $this->delUploadFile('gallery',$config['prefix'].$filename);
              echo 1;
            }           
          }
      die;
  } 

  /**
   * 保存上传的图片
   * @access protected
   * @param int $id
   * @param string $table 
   */
  protected function saveGallery($id,$table)
  {
      $gallery=M($table);
      $time=time();
      $filename=$this->_post('filename');
      $filedesc=$this->_post('filedesc');
      $sortOrder=$this->_post('sort_order');
      $productId=$id;
      $gallery->where(array('product_id'=>$id))->delete();
      foreach($filename as $k=>$v)
      {
        $data['filename']=$v;
        $data['filedesc']=$filedesc[$k];
        $data['sort_order']=$sortOrder[$k];
        $data['product_id']=$productId;
        $data['upload_time']=$time;
        $gallery->add($data);
      }    
  }

  /**
   * 根据ID删除图片
   * @access protected
   * @param int $id
   * @param string $table 
   */
  protected function delGalleryById($id,$table)
  {
    $config=C('GALLERY_THUMB_SIZE');
    $gallery=M($table);
    $where['product_id']=array('in',$id);
    $files=$gallery->field('filename')->where($where)->select();
    foreach($files as $v)
    {
      $this->delUploadFile('thumb',$config['prefix'].$v['filename']);
      $this->delUploadFile('gallery',$v['filename']);            
    }
    $gallery->where($where)->delete();
  }


}
?>